$(function () {
    //分页
    function pages() {
        //获取页面数
        var books = $(".bookItem").length;
        var pages = Math.ceil(books / 8);
        $(".total").html('共' + pages + '条');
        console.log(pages);
        return pages;
    }

    //为图书设置index
    function bookIndex() {
        var books = $(".bookItem").length;
        var pages = Math.ceil(books / 8);
        var bookLi = document.getElementsByClassName("bookItem");
        for (var i = 0; i < pages; i++) {
            for (var j = 0; j < 8; j++) {
                if (j + i * 8 == books) break;
                bookLi[j + i * 8].setAttribute("index", i + 1);
            }
        }
    }

    //初始化
    bookIndex();
    pages();

    //book与页面index相等时显示
    function showBook() {
        var index = $(".current-page").attr("index");
        var bookLi = document.getElementsByClassName("bookItem");
        for (var i = 0; i < bookLi.length; i++) {
            //先全部隐藏
            bookLi[i].style.display = 'none';
            //index符合时显示
            if (bookLi[i].getAttribute("index") == index) bookLi[i].style.display = 'inline-block';
        }
    }

    //初始化
    showBook();

    //上下页按钮
    $("#prev").on("click", function () {
        var index = $(".current-page").attr("index");
        if (index > 1) {
            index--;
            $(".current-page").attr("index", index);
            $(".current-page").html('第' + index + '页');
        }
        showBook();
    })

    $("#next").on("click", function () {
        var index = $(".current-page").attr("index");
        console.log(pages());
        if (index < pages()) {
            index++;
            $(".current-page").attr("index", index);
            $(".current-page").html('第' + index + '页');
        }
        showBook();
    })

    //加图片
    function AddPic() {
        var bookLi = document.getElementsByClassName("bookItem");
        for (var i = 0; i < bookLi.length; i++) {
            var name = bookLi[i].id;

            bookLi[i].style.backgroundImage = 'url(/images/' + name + '.jpeg)';
        }
    }

    AddPic();

    var $search_input = $('#search-input'),
        $auto_complete = $('#auto-complete');

    //搜索框自动补全
    $search_input.on('keyup', function () {
        var keyword = $(this).val();
        if (keyword !== '' && keyword !== null && keyword.length !== 0) {
            $.ajax({
                type: 'GET',
                url: 'editBook',
                data: {'type': 3, 'keyword': keyword},
                success: function (data) {
                    console.log(data);
                    var bookListHTML = '';
                    for (var i = 0; i < data.length; i++) {
                        bookListHTML += '<li data-bookName="' + data[i].bookName + '">' + data[i].bookName + ' : ' + data[i].bookAuthor + '</li>';
                    }
                    $('#auto-complete-list').html(bookListHTML);
                    $auto_complete.show();
                },
                error: function (errorMsg) {
                    console.log(errorMsg);
                }
            });
        } else {
            $auto_complete.hide();
        }
    });
    $(document).on('click', '.auto-complete-list li', function () {
        $auto_complete.hide();
        $(".current-page").attr("index", 1);
        $(".current-page").html('第1页');
        var bookName = $(this).data('bookname');
        //刷新下方表格，筛出这本书
        $.ajax({
            type: 'GET',
            url: 'editBook',
            data: {'type': 4, 'liBookName': bookName},
            success: function (data) {
                //清空表格
                $('.bookItem').remove();
                //显示该书
                var innerHTML = '';
                innerHTML += '   <a href="bookDetails?bookId=' + data.bookId + '"><li class="bookItem" id="book' + data.bookId + '">' +
                    data.bookName +
                    '           </li></a>';

                $('.item').append(innerHTML);

                bookIndex();
                AddPic();
                pages();
                showBook();

                //清空input
                $('#search-input').val("");

            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        })
    });
    //点击搜索按钮
    $('#search-btn').on('click', function () {
        $auto_complete.hide();
        var keyword = $search_input.val();
        $(".current-page").attr("index", 1);
        $(".current-page").html('第1页');
        //刷新下方表格，筛出这些书
        $.ajax({
            type: 'GET',
            url: 'editBook',
            data: {'type': 3, 'keyword': keyword},
            success: function (data) {
                console.log(data);
                //清空表格
                $('.bookItem').remove();
                //显示这些书
                var innerHTML = '';
                for (var i = 0; i < data.length; i++) {
                    innerHTML += '   <a href="bookDetails?bookId=' + data[i].bookId + '"><li class="bookItem" id="book' + data[i].bookId + '">' +
                        data[i].bookName +
                        '           </li></a>';
                }

                $('.item').append(innerHTML);

                bookIndex();
                AddPic();
                pages();
                showBook();

                //清空input
                $('#search-input').val("");
            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        })
    });

    //跳转确定按钮
    $('.nav-bottom .btn').on("click", function () {
        var index = $(".target-page").val();
        console.log(index);
        if (index < 1 || index == '' || index > pages()) {
            alert("输入错误")
            $(".target-page").val("");
        } else {
            $(".current-page").attr("index", index);
            showBook();
            $(".target-page").val("");
        }
    })

})

